// 04myCenter/01myWallet/02enterpriseChange/enterprisebankCard/addEnterprisebankCard/addEnterprisebankCard.js
var co = require('../../../../../resource/js/common.js')
var app = getApp()
var _this, _data, _opt = {}

Page({
   data: {
      name: '', //判断是否输入名字
      accountNo: '', //判断是否输入银行卡号
      Bank_Name: '', //判断是否输入所属银行
      idCardCode: '', //判断是否输入身份证号
      bankPreMobile: '', //判断是否输入预留电话     
      modal: '', //所属银行模态框开关
      indexs: '', //所选银行的小标
      bank: [ //微信文档中支持转账的银行
         {
            name: '工商银行',
            id: 1002
         },
         {
            name: '农业银行',
            id: 1005
         },
         {
            name: '中国银行',
            id: 1026
         },
         {
            name: '建设银行',
            id: 1003
         },
         {
            name: '招商银行',
            id: 1001
         },
         {
            name: '邮储银行',
            id: 1066
         },
         {
            name: '交通银行',
            id: 1020
         },
         {
            name: '浦发银行',
            id: 1004
         },
         {
            name: '民生银行',
            id: 1006
         },
         {
            name: '兴业银行',
            id: 1009
         },
         {
            name: '平安银行',
            id: 1010
         },
         {
            name: '中信银行',
            id: 1021
         },
         {
            name: '华夏银行',
            id: 1025
         },
         {
            name: '广发银行',
            id: 1027
         },
         {
            name: '光大银行',
            id: 1022
         },
         {
            name: '北京银行',
            id: 1032
         },
         {
            name: '宁波银行',
            id: 1056
         },
      ],

      //legalPersonName: '', //法人姓名
      //busLicenseName: '', // 商家营业执照名称
      userName: "",//用户名字
      IDCard: '', //身份证号码
      phoneNum: '', //手机号码
      tempPhone: '', //验证手机号
      smsCode: '', //验证码
      disabled: false, //验证码按钮状态
      time: '获取验证码', //验证码倒计时
      countTime: 60, //倒计时计时时间

      isShow: false, //是否显示
      wChatImg: '', //微信头像
      wChatName: '', //微信名称
      isFromBankCard: false, //是否来自银行卡页面 
   },

   onLoad: function (opt) {
      _this = this;
      _data = this.data;
      console.log('app.Data',app.Data)
      //console.log('userInfo===', app.Data.userInfo)

      // var Arr = opt.scene.split('.')
      // var len = Arr.length / 2
      // console.log('Arr===',Arr)
      // console.log('len===',len)
      // for (var i = 0; i < len; i++) {
      //    var val_i = i * 2 + 1
      //    var key = Arr[val_i - 1]
      //    _opt[key] = Arr[val_i]
      // }

      _this.getBankInfo()
   },

   onUnload: function () {
      _this = null
      _data = null
      _opt = {}
   },

   //获取银行实名信息
   getBankInfo: function () {
      co.getShareApi('bankcard/ba/getInfo', {
         User_ID: app.Data.userInfo.User_ID
      }, function (res) {
         if (res.code == 0) {
            _this.setData({
               userName: res.data.Account_Name,
               IDCard: res.data.ID_Card_No
            })
         }
      })
   },

   formSubmit: function (e) {
      var param = e.detail.value
      var smsCode = _this.data.smsCode
      console.log("sms", smsCode)
      var tempPhone = this.data.tempPhone
      //param.Biz_ID = app.Data.shopInfo.Biz_ID
      //param.Biz_ID = _opt.Biz_ID
      console.log('【param】===========', param)

      var name = '',
         accountNo = '',
         idCardCode = '',
         bankPreMobile = '',
         Bank_Name = '',
         telRule = /^1[0-9]{10}$/,
         verificationCode = '',
         isHint = 0;
      this.setData({
         name: name,
         accountNo: accountNo,
         idCardCode: idCardCode,
         bankPreMobile: bankPreMobile,
         Bank_Name: Bank_Name,
         verificationCode: verificationCode
      })

      if (co.empty(param.Account_Name)) {
         name = "*请输入用户名";
         isHint = 1
      }
      if (co.empty(param.Account_No)) {
         accountNo = "*请输入银行卡号";
         isHint = 1
      }
      if (co.empty(param.ID_Card_No)) {
         idCardCode = "*请输入身份证号码";
         isHint = 1
      }
      if (co.empty(param.Mobile)) {
         bankPreMobile = "*请输入手机号";
         isHint = 1
      }
      if (!telRule.test(param.Mobile)) {
         bankPreMobile = "*手机号格式不正确";
         isHint = 1
      }
      if (co.empty(param.Bank_Name)) {
         Bank_Name = "*请选择所属银行"
      }
      if (!_data.IDCard) {
         console.log("后台掩码", smsCode)
         console.log("shurukuang", param.Sms_Code)
         if (smsCode != param.Sms_Code) {
            verificationCode = '验证码错误'
            isHint = 1
         }
         if (tempPhone != param.Mobile) {
            verificationCode = '验证码已过期'
            isHint = 1
         }
      }

      if (isHint == 1) {
         this.setData({
            name: name,
            accountNo: accountNo,
            idCardCode: idCardCode,
            bankPreMobile: bankPreMobile,
            Bank_Name: Bank_Name,
            verificationCode: verificationCode
         })
      } else {
         wx.showLoading({
            title: '认证中',
         })

         this.bankCardCheck(param)
      }
   },

   // 银行卡验证
   bankCardCheck: function (param) {
      param.Bank_Code = this.data.bank[this.data.indexs].id;
      param.User_ID = app.Data.userInfo.User_ID
      console.log(param)
      co.getShareApi("api/Idcard/bankCardCheck", param, function (res) {
         console.log("【res=============】", res)
         wx.hideLoading()
         co.Modal(res.msg)
         if (res.code == 0) {
            //app.Data.shopInfo.BankCard_Is_Auth = 1
            app.getShopInfo(function (res) { })
            setTimeout(function () {
               wx.navigateBack({})
            }, 1000)
         }
      })
   },

   //商家认证保存(法人认证保存)
   // saveAuthLegalPerson: function (param) {
   //    param.Bank_Code = this.data.bank[this.data.indexs].id;
   //    param.User_ID = app.Data.userInfo.User_ID
   //    co.getShareApi("biz/Bmg/saveAuthLegalPerson", param, function (res) {
   //       console.log("【res=============】", res)
   //       wx.hideLoading()
   //       if (res.code == 0) {
   //          //app.Data.shopInfo.BankCard_Is_Auth = 1
   //          //app.getShopInfo()
   //          // setTimeout(function() {
   //          //    wx.navigateBack({})
   //          // }, 1000)
   //          co.confirm('', res.msg, '取消', '确定', function (res1) {
   //             console.log('_opt.Biz_ID====', _opt.Biz_ID)
   //             if (res1) {
   //                app.getShopInfo(function (res) {
   //                   wx.redirectTo({
   //                      url: '/03admin/12bizAuth/bizAuth?Biz_ID=' + _opt.Biz_ID,
   //                   })
   //                }, _opt.Biz_ID)
   //             }
   //          })
   //       } else {
   //          co.Modal(res.msg)
   //       }
   //    })
   // },

   // 选择银行卡所属银行
   radioChange: function (e) {
      this.setData({
         indexs: e.detail.value,
         modal: false
      })
      console.log(this.data.indexs)
   },

   selectBank: function () {
      this.setData({
         modal: true
      })
   },

   // 获取手机号
   getPhoneNumber: function (e) {
      var phone = e.detail.value //输入的手机号码存入
      this.data.phoneNum = phone
   },

   // 获取验证码
   getVerCode: function () {
      var inPhone = this.data.phoneNum //输入手机号
      var telRule = /^1[0-9]{10}$/ //手机号码格式
      var bankPreMobile = ''

      if (inPhone == '') {
         bankPreMobile = '请输入手机号码!'
      } else if (!telRule.test(inPhone)) {
         bankPreMobile = '手机号码格式错误'
      }

      if (bankPreMobile != '') {
         this.setData({
            bankPreMobile: bankPreMobile,
         })
         return
      }

      //倒计时
      this.countDownTime();
      this.setData({
         disabled: true
      })

      co.getShareApi('api/Sms/getPhoneCode', {
         mobile: inPhone,
         appid: 1400134010,
         type: 'biz_auth'
      }, function (res) {
         _this.data.smsCode = res.data.Sms_Code
      })
      this.data.tempPhone = inPhone
   },

   //获取短信验证码倒计时
   countDownTime: function (options) {
      var countTime = this.data.countTime
      //启动时间计时
      var interval = setInterval(function () {
         countTime--;
         _this.setData({
            time: countTime + '秒'
         })
         if (countTime <= 0) {
            //关闭时间计时
            clearInterval(interval)
            _this.setData({
               time: '重新获取',
               disabled: false,
            })
            _this.data.currentTime = 60
            //_this.data.smsCode = ''
         }
      }, 1000)
   },

   // //取消
   // cancel: function (e) {
   //    wx.navigateBack({})
   // },

})